import Vue from 'vue'
import Router from 'vue-router'
import index from './router'
import login from '../login/index'
import WX from '../wx/index'

Vue.use(Router)
const router = new Router({
  routes: index
})
router.beforeEach((to, from, next) => {
  var vstr = window.location.href
  var reg = new RegExp('\\?.*#', 'g')
  if (vstr.search(reg) !== -1) {
    window.location.href = vstr.replace(reg, '#')
  }
  // if (window.location.host === 'readingh5.qingclass.com') {
  //     return window.location.href = window.location.href.replace('https://readingh5.qingclass.com/TenVip/index.html#/', 'https://reading.szhfy.top/TenVip/index.html#/')
  // } else if (window.location.host === 'readh5.qingclass.com') {
  //     return window.location.href = window.location.href.replace('http://readh5.qingclass.com/TenVip/index.html#/', 'http://readingtest.szhfy.top/TenVip/index.html#/')
  // }
  if (window.location.host === 'reading.szhfy.top') {
    return window.location.href = window.location.href.replace('https://reading.szhfy.top/TenVip/index.html#/', 'https://readingh5.qingclass.com/TenVip/index.html#/')
  } else if (window.location.host === 'readingtest.szhfy.top') {
    return window.location.href = window.location.href.replace('http://readingtest.szhfy.top/TenVip/index.html#/', 'http://readh5.qingclass.com/TenVip/index.html#/')
  }
// 路由发生变化修改页面title
  if (to.fullPath === from.fullPath) {
    next()
    return
  }

  if (to.meta.title) {
    document.title = to.meta.title
  }

  if (to.matched.some(m => m.meta.needGuard)) {
    guardRoute(to, from, next)

  } else {
    next()
  }
  var vstr = window.location.href
  var reg = new RegExp('\\?.*#', 'g')
  if (vstr.search(reg) !== -1 && to.path === '/BuyVip') {
    window.location.href = vstr.replace(reg, '#')
  }
  if (to.path === '/wx_auth_done') {
    window.localStorage.setItem('platform_user_info', JSON.stringify(to.query))
    login.oauth_token = to.query
    login.sign(to.query, res => {
      sessionStorage.removeItem('wx_auth')
      login.Vip_token.vip_user_id = res.vip_user_id
      login.Vip_token.reading_vip_state = res.is_vip
      login.Vip_token.vip_token1 = res.vip_token1
      login.Vip_token.vip_token2 = res.vip_token2
      window.localStorage.setItem('Ten_Vip_token', JSON.stringify(login.Vip_token))
      next({path: localStorage.getItem('pre_page') || '/'})
    })
  } else {
    // login.oauth_token.previous_page = window.location.href.split('#')[1]
    // window.localStorage.setItem('platform_user_info', JSON.stringify(login.oauth_token))
    if (to.path === '/VipCard' || to.path === '/Answer') {
      // login.shares()
      WX.hideMenuItems()
    } else {
      WX.showMenuItems()
    }
    if (to.path !== '/TestResult' && to.path !== '/Promotion' && to.path !== '/PromCard' && to.path !== '/TestInfo' && to.path !== '/VipCard') {
      login.shares()
    }
    if (to.path === '/Collection' && !to.query.share_id && !to.query.share_token) {
      sessionStorage.setItem('querye', JSON.stringify(to.query))
    }
    next()
  }
})

export default router
